package com.ljh.myaiagent.tools;

import jakarta.annotation.PostConstruct;
import jakarta.mail.*;
import jakarta.mail.internet.InternetAddress;
import jakarta.mail.internet.MimeMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;

import java.util.Properties;

@Slf4j
public class SendQQEmailTool {
    private final String FROM_EMAIL;
    private final String AUTH_CODE;
    private static final String SMTP_HOST = "smtp.qq.com"; // QQ邮箱的SMTP服务器地址
    private static final String SMTP_PORT = "587"; // QQ邮箱的SMTP服务器端口号

    public SendQQEmailTool(String FROM_EMAIL, String AUTH_CODE) {
        this.FROM_EMAIL = FROM_EMAIL;
        this.AUTH_CODE = AUTH_CODE;
    }
    /**
     * 邮件发送：实现给用户发送邮件的功能
     */
    @Tool(description = "Send a TEXT formatted email to a specified recipient with a subject and body.")
    public String sendTextEmail(@ToolParam(description = "The email address of the recipient.") String to,
                            @ToolParam(description = "The subject of the email.") String subject,
                            @ToolParam(description = "The body content of the email.") String body){
        // 实现文字邮件发送功能
        Properties properties = new Properties();
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.host", SMTP_HOST);
        properties.put("mail.smtp.port", SMTP_PORT);
        properties.put("mail.smtp.starttls.enable", "true");
        Session session = Session.getInstance(properties, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(FROM_EMAIL, AUTH_CODE);
            }
        });
        try {
            MimeMessage mimeMessage = new MimeMessage(session);
            mimeMessage.setFrom(FROM_EMAIL);
            mimeMessage.setRecipients(MimeMessage.RecipientType.TO, InternetAddress.parse(to));
            mimeMessage.setSubject(subject);
            mimeMessage.setText(body);
            Transport.send(mimeMessage);
            return "Email sent successfully to " + to + " with subject: " + subject;
        } catch (MessagingException e) {
            return "Error sending email: " + e.getMessage();
        }
    }

    @Tool(description = "Send an HTML formatted email to a specified recipient with a subject and body.")
    public String sendHtmlEmail(@ToolParam(description = "The email address of the recipient.") String to,
                                @ToolParam(description = "The subject of the email.") String subject,
                                @ToolParam(description = "The body content of the email.") String body){
        // 实现html邮件发送功能
        Properties properties = new Properties();
        properties.put("mail.smtp.auth", "true");
        properties.put("mail.smtp.host", SMTP_HOST);
        properties.put("mail.smtp.port", SMTP_PORT);
        properties.put("mail.smtp.starttls.enable", "true");
        Session session = Session.getInstance(properties, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(FROM_EMAIL, AUTH_CODE);
            }
        });
        try {
            MimeMessage mimeMessage = new MimeMessage(session);
            mimeMessage.setFrom(FROM_EMAIL);
            mimeMessage.setRecipients(MimeMessage.RecipientType.TO, InternetAddress.parse(to));
            mimeMessage.setSubject(subject);
            mimeMessage.setContent(body, "text/html; charset=utf-8");
            Transport.send(mimeMessage);
            return "Email sent successfully to " + to + " with subject: " + subject;
        } catch (MessagingException e) {
            return "Error sending email: " + e.getMessage();
        }
    }
}
